<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/30
 * Time: 17:24
 */

namespace app\api\model;


class Message extends BaseModel
{

    /**
     * 与user一对一关联
     * @return \think\model\relation\BelongsTo
     */
    public function fromuser(){
        return $this->belongsTo('User','from_uid','id');
    }
    /**
     * 与WorkerUser一对一关联
     * @return \think\model\relation\BelongsTo
     */
    public function fromworkeruser(){
        return $this->belongsTo('WorkUser','from_wu_id','id');
    }

    /**
     * 与user一对一关联
     * @return \think\model\relation\BelongsTo
     */
    public function touser(){
        return $this->belongsTo('User','to_uid','id');
    }

    /**
     * 与WorkerUser一对一关联
     * @return \think\model\relation\BelongsTo
     */
    public function toworkeruser(){
        return $this->belongsTo('WorkUser','to_wu_id','id');
    }
    /**
     * 聊天记录入库
     * @param $data
     * @return int|string
     */
    public static function insertCord($data){
      return self::insert($data);
    }

    /**
     * 获取最新的聊天记录
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getCord($wu_id){
        $result =  self::with(['fromuser'])
            ->where('is_read','=',0)
            ->where('to_wu_id','=',$wu_id)
            ->order("create_time desc")
            ->field('msg_type,content,max(create_time),from_uid')
            ->group('from_uid')
            ->select();
        if (!$result->isEmpty()){
            foreach ($result as $value ){
                $info = $value->visible(['msg_type','content','max(create_time)','fromuser']);
                $info->fromuser->visible(['id','nickname','avatar_url']);
            }
        }
        return $result;
    }

    /**
     * 获取未读消息条数
     * @param $wu_id
     * @return int|string
     */
    public static function  getCount($wu_id){
        $result =  self::where('is_read','=',0)
            ->where('from_id','=',$wu_id)
            ->count();
        return $result;
    }
    /**
     * 获取聊天记录表
     * @param $uid
     * @param $wu_id
     * @param $page
     * @param $size
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getMessageList($uid,$wu_id,$page,$size){
        $result =  self::with(['fromuser'])
            ->with(['fromworkeruser'=>['userimg']])
            ->with(['touser'])
            ->with(['toworkeruser'=>['userimg']])
            ->where("to_uid = $uid and from_wu_id = $wu_id")
            ->whereOr(" to_wu_id = $wu_id and from_uid = $uid ")
            ->field('msg_type,content,create_time,from_uid,from_wu_id,to_uid,to_wu_id,timeid,is_read')
            ->order("create_time desc")
            ->limit(($page-1)*$size,$size)
            ->select();
        if (!$result->isEmpty()){
            foreach ($result as $value ){
                $info = $value->visible(['msg_type','content','create_time','from_uid','from_wu_id','to_uid','to_wu_id','timeid','is_read','fromuser','fromworkeruser','touser','toworkeruser']);
                if (!empty($info->fromuser)){
                    $info->fromuser->visible(['id','nickname','avatar_url']);
                }
                if (!empty($info->fromworkeruser)){
                    $info->fromworkeruser->visible(['id','name','userimg']);
                }
                if (!empty($info->touser)){
                    $info->touser->visible(['id','nickname','avatar_url']);
                }
                if (!empty($info->toworkeruser)){
                    $info->toworkeruser->visible(['id','name','userimg']);
                }
            }
        }
        return $result;
    }

    /**
     * 沟通用户总数
     * @param $t_id
     * @return int|string
     */
    public static function getAllUser($t_id,$time){
        $work_user = WorkUser::where('t_id','=',$t_id)->field('id')->select();
        $ids = array_column($work_user->toArray(),'id');
        $num = self::where('from_wu_id','in',$ids)
            ->where('create_time','>',$time)
            ->field('distinct to_uid')
            ->select();
        return count($num);
    }
    /**
     * 改变已读
     * @param $timeids
     * @return $this
     */
    public  function changeStatus($timeids,$type){
        $data['is_read'] = 1;
        if ($type == 'user'){
            $status = self::where('timeid','in',$timeids)
                ->where('from_uid = 0')->update($data);
        }elseif($type == 'workuser'){
            $status = self::where('timeid','in',$timeids)
                ->where('from_uid != 0')->update($data);
        }
        return $status;
    }
}